import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const state = {
    user:{
        
    },
    token:'',
    nav:'',//左侧状态栏的值
    wsUrl:'',
    socket:'',
    open:'',
    error:'',
    close:'',
    message:'',
    chatList:'',//聊天列表
    friendList:[],//好友列表
    groupList:[],//群聊列表
    groupMembersList:[],//群成员列表
    groupRemarks:'',//群简介
    groupNotice:'',//群公告
    groupName:'',//群昵称
    groupImg:'',//群头像
    choosedGroupId:'',//被选中的群Id
    isQunzhu:'',//是否为选中群的群主
    isGuanli:'',//是否为选中群的管理
    chatChooseItem:'',//聊天选中的项的数据
    chooseCardItem:'',
}

const mutations = {
    //获取token
    getToken(state,token){
        state.token = token
    },
    //设置个人信息
    setUserInfo(state,obj){
        state.user = {
            ...state.user,
            ...obj
        }
        if(state.chatList&&state.chatList.length!=0){
            state.chatList.forEach((element,index)=>{
                if(state.chatChooseItem.type==1&&state.user.id==element.toId){
                    element.toUserImg = state.user.headImg
                    element.toUserName = state.user.userName
                }
            })
        }
    },
    //切换左侧nav的值
    setNav(state,str){
        state.nav = str
    },
    //获取卡片的数据
    getCardItem(state,obj){
        state.chooseCardItem = obj
    },
    //获取新聊天消息列表
    getChatList(state,arr){
        console.log('聊天列表',arr)
        arr.forEach((element,index)=>{
            element.choose = false
            if(element.sendUserId!=state.user.id&&element.type==1){
                var toId = element.toId
                var toUserImg = element.toUserImg
                var toUserName = element.toUserName

                element.toId = element.sendUserId
                element.toUserImg = element.sendUserImg
                element.toUserName = element.sendUserName

                element.sendUserId = toId
                element.sendUserImg = toUserImg
                element.sendUserName = toUserName
            }else if(element.type==0){
                element.sendUserId = state.user.id
                element.sendUserImg = state.user.headImg
                element.sendUserName = state.user.userName
            }
        })
        state.chatList = arr
    },
    //重置新聊天消息列表
    setChatList(state,obj){
        var chatEle = ''
        var hadChat = false
        state.chatList.forEach((element,index)=>{
            // element.choose = false
            // if(obj.myselfChoose){
            //     element.choose = false
            // }
            if(element.toId == obj.toId&&element.type == obj.type){
                element = obj
                // element.choose = true
                // if(obj.myselfChoose){
                //     element.choose = true
                //     element.myselfChoose = false
                // }
                chatEle = element
                state.chatList.splice(index,1)
                hadChat = true
            }
        })
        if(!hadChat){
            state.chatList.unshift(obj)
        }else{
            state.chatList.unshift(chatEle)
        }
        console.log('聊天列表',state.chatList)

        state.chatList.forEach((element,index)=>{
            element.index = index
        })
    },
    //选择聊天
    chooseTalk(state,index){
        state.chatList.forEach((element)=>{
            element.choose = false
        })
        if(state.chatList.length!=0){
            state.chatList[index].choose = true
        }
    },
    //获取好友列表
    getFriendsList(state,arr){
        console.log('好友列表',arr)
        state.friendList = arr
    },
    //获取群聊列表
    getGroupList(state,arr){
        console.log('群列表',arr)
        state.groupList = arr
    },
    //获取群成员列表,判断自己是否为群主或群管
    getGroupMembersList(state,arr){
        console.log('群成员列表',arr)
        state.groupMembersList = arr
        
        state.isQunzhu=false
        state.isGuanli=false
        state.groupMembersList.forEach((element)=>{
            if(element.userId==state.user.id){
                if(element.userRole==99){
                    state.isQunzhu = true
                }else if(element.userRole==1){
                    state.isGuanli = true
                }
            }
        })
    },
    //获取聊天选中项的数据
    getChatChooseItem(state,item){
        state.chatChooseItem = item
    },
    //修改群信息
    changeGroupBase(state,obj){
        state.groupList.forEach((element)=>{
            if(element.id==state.choosedGroupId){
                if(obj.groupName){
                    state.groupName = obj.groupName
                }
                if(obj.groupNotice){
                    state.groupNotice = obj.groupNotice
                }
                if(obj.groupRemarks){
                    state.groupRemarks = obj.groupRemarks
                }
                element.roomName = state.groupName
                element.roomNotice = state.groupNotice
                element.remarks = state.groupRemarks
            }
        })
        state.chatList.forEach((ele)=>{
            state.groupList.forEach((element)=>{
                if(ele.type===0&&(ele.toId==element.id)){
                    ele.toUserImg = element.roomImg
                    ele.toUserName = element.roomName
                    if(ele.toId==state.chatChooseItem.toId){
                        state.chatChooseItem.toUserName = ele.toUserName
                    }
                }
            })
        })
    },
    //修改群头像
    changeGroupImg(state,img){
        state.groupList.forEach((element)=>{
            if(element.id==state.choosedGroupId){
                element.roomImg = img
            }
        })
        state.groupImg = img

        state.chatList.forEach((ele)=>{
            state.groupList.forEach((element)=>{
                if(ele.type===0&&(ele.toId==element.id)){
                    ele.toUserImg = element.roomImg
                    ele.toUserName = element.roomName
                }
            })
        })
    },
    //选中了某个群
    chooseGroup(state,roomId){
        state.groupList.forEach((element)=>{
            if(element.id==roomId){
                state.groupRemarks = element.remarks
                state.groupNotice = element.roomNotice
                state.groupName = element.roomName
                state.groupImg = element.roomImg
                state.choosedGroupId = roomId
            }
        })
    },
    //修改好友备注
    changeFriendRemark(state,obj){
        state.friendList.forEach((element)=>{
            if(element.friendId==obj.friendId){
                element.friendRemarks = obj.friendRemarks
            }
        })
    },
    //删除好友
    deleteFriend(state,friendId){
        state.friendList.forEach((element,index)=>{
            if(element.friendId==friendId){
                state.friendList.splice(index,1)
            }
        })
    },
    //获取消息
    getMessage(state,msg){
        state.message = msg
    },
    //设置websocket
    setWsUrl(state,url){
        state.wsUrl = url
        state.socket = new WebSocket(url)
        
        state.socket.onopen = function(e){
            //当WebSocket创建成功时，触发onopen事件
            console.log("连接成功");
        }
        state.socket.onmessage = function(e){
        　　//当客户端收到服务端发来的消息时，触发onmessage事件，参数e.data包含server传递过来的数据
        　　state.message = JSON.parse(e.data);
            console.log('stateMessage',state.message)
        }
        state.socket.onclose = function(e){
        　　//当客户端收到服务端发送的关闭连接请求时，触发onclose事件
        　　console.log("close");
        }
        state.socket.onerror = function(e){
        　　//如果出现连接、处理、接收、发送数据失败的时候触发onerror事件
        　　console.log(error);
        }
    }
}

export default new Vuex.Store({
    state,
    mutations
})